﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatabaseAccess;
using System.Data;
using System.Data.SqlClient;

namespace BusinessLayer
{
    public class AccountBusinessLayer
    {
        public IEnumerable<Account> Accounts
        {
            get
            {
                    string connectionString = MyConnection.openConnection();
                    List<Account> accounts = new List<Account>();
                try
                {
                    
                    using (SqlConnection con = new SqlConnection(connectionString))
                    {
                        SqlCommand cmd = new SqlCommand("spGetAllAccounts", con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        con.Open();
                        SqlDataReader rdr = cmd.ExecuteReader();
                        while (rdr.Read())
                        {
                            Account account = new Account();
                            account.Account_Id = Convert.ToInt32(rdr["Account_Id"]);
                            account.Account_Name = rdr["Account_Name"].ToString();
                            account.Account_Pass = rdr["Account_Pass"].ToString();
                            account.Account_Email = rdr["Account_Email"].ToString();
                            account.Account_Role = rdr["Account_Role"].ToString();
                            account.Account_Status = Convert.ToInt32(rdr["Account_Status"]);
                            account.Date_Register = Convert.ToDateTime(rdr["Date_Register"]);
                            account.Last_Login = Convert.ToDateTime(rdr["Last_Login"]);
                            account.Login_Count = Convert.ToInt32(rdr["Login_Count"]);

                            accounts.Add(account);
                        }
                    }
                }
                catch (Exception e)
                {
                    
                    throw new Exception(e.ToString());
                }
                return accounts;
            }
              
        }
    }
}
